package com.剑指offer;

import java.util.ArrayDeque;
import java.util.Queue;

public class 判断完全二叉树
{
    public static boolean isCompleteTree(TreeNode root)
    {
        if (root == null)
        {
            return false;
        }
        boolean flag = false;
        Queue<TreeNode> queue = new ArrayDeque<>();
        queue.offer(root);
        while (!queue.isEmpty())
        {
            TreeNode temp = queue.poll();
            if (temp.left == null)
            {
                if (temp.right != null)
                {
                    return false;
                }
                if (temp.right == null)
                {
                    flag = true;
                }
            }
            else
            {
                if (flag == true)
                {
                    return false;
                }
                if (temp.right == null)
                {
                    flag = true;
                    queue.add(temp.left);
                }
                if (temp.right != null)
                {
                    queue.add(temp.left);
                    queue.add(temp.right);
                }
            }
        }

        return true;
    }
}
